# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#
$ ->
    form = $('form.ui.form');

    i_username = form.find 'input[name="username"]'
    last_login = Cookies.get 'last.login.username'
    i_username.val last_login
    if i_username.val() == ""
        i_username.focus()
    else
        form.find('input[name="password"]').focus()

    form.form fields:
        username:
            rules: [{ type : 'empty', prompt : '必填' }]
        password:
            rules: [{ type : 'empty', prompt : '必填' }]

    form.submit ->
        Cookies.set 'last.login.username',
            form.find('input[name="username"]').val(),
            expires: 365, path: '/'
        return true

    # 隐藏回显消息
    echo_close = $('#echo_close')
    if echo_close
        echo_close.click ->
            $(this).parent("#echo_message").addClass('hidden')

    #
    if form.find('.submit.button').data('locked')
        update_text = ->
            button = form.find '.submit.button'
            wait_time = button.data 'wait-time'
            minutes = Math.floor wait_time / 60
            seconds = wait_time % 60
            button.text "将在 #{minutes} 分 " + "#{seconds} 秒后解锁，请等待..."
            button.data 'wait-time', wait_time - 1
            window.location = '/login' if parseInt(wait_time) <= 0
        setInterval update_text, 1000
